package com.bw.finance.service.impl;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bw.finance.conf.ResponseResult;
import com.bw.finance.eneity.cms.EduWebsitePage;
import com.bw.finance.eneity.cms.EduWebsiteTemplate;
import com.bw.finance.mapper.EduWebsitePageMapper;
import com.bw.finance.service.IEduWebsitePageService;
import com.bw.finance.service.IEduWebsiteTemplateService;
import com.bw.finance.utils.PhotoUtils;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zxx
 * @since 2020-08-16
 */
@Service
public class EduWebsitePageServiceImpl extends ServiceImpl<EduWebsitePageMapper, EduWebsitePage> implements IEduWebsitePageService {
    @Autowired
    private EduWebsitePageMapper pageMapper;

    @Autowired
    private IEduWebsitePageService pageService;

    @Autowired
    private IEduWebsiteTemplateService templateService;

    @Autowired
    private RestTemplate restTemplate;

    @Override
    public List<EduWebsitePage> findlist(Map map) {
        return pageMapper.findlist(map);
    }

    @Override
    @Transactional
    public ResponseResult addpage(Long id, HttpServletResponse response) {
        ResponseResult result = ResponseResult.SUCCESS();
        try{
            EduWebsitePage page = pageService.getById(id);
            EduWebsiteTemplate template = templateService.getById(page.getTemplateId());
            Configuration configuration = new Configuration(Configuration.getVersion());
            ClassPathResource classPathResource = new ClassPathResource("Lunbo");
            configuration.setDirectoryForTemplateLoading(classPathResource.getFile());
            configuration.setDefaultEncoding("utf-8");
            Template template1 = configuration.getTemplate(template.getName()+".html");
            List list = restTemplate.getForObject(page.getDataUrl(), List.class);
            template.getName();
            HashMap map = new HashMap();
            map.put("ab", JSON.toJSONString(list));
            String templateIntoString = FreeMarkerTemplateUtils.processTemplateIntoString(template1, map);
            InputStream inputStream = IOUtils.toInputStream(templateIntoString, "utf-8");
            FileOutputStream outputStream = new FileOutputStream("G:\\1711a\\finance01\\code\\C\\financeC\\"+ template.getName() + ".html");
            IOUtils.copy(inputStream,outputStream);
            page.setPubTime(new Date());
            page.setUpdateTime(new Date());
            page.setStatus(1);
            pageService.saveOrUpdate(page);
            outputStream.close();
            inputStream.close();
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("上传失败");
        }
        return result;
    }
}
